package org.springframework.core.io;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * 默认的资源加载器
 */
public class DefaultResourceLoader implements ResourceLoader {
    private static final String CLASSPATH_URL_PREFIX = "classpath:";

    @Override
    public Resource getResource(String location) {
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            //说明是classpath下的资源
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        } else {
            try {
                //不是类路径下的 尝试当成url来处理
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                //当成文件系统下的资源处理 好像不需要下面这个修改path的步骤
//                String path = location;
//                if (location.startsWith("/")) {
//                    path = location.substring(1);
//                }
                return new FileSystemResource(location);
            }

        }
    }
}
